package com.rede.didiok.admin.restapi.admin;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.rede.didiok.common.constant.conf.RedisConf;
import com.rede.didiok.common.constant.conf.SysConf;
import com.rede.didiok.common.constant.global.Constants;
import com.rede.didiok.common.dto.rank.RankCountResultDto;
import com.rede.didiok.common.dto.user.UserCountResultDto;
import com.rede.didiok.common.feign.RankFeignClient;
import com.rede.didiok.common.feign.UserFeignClient;
import com.rede.didiok.common.utils.RedisUtil;
import com.rede.didiok.common.utils.ResultUtil;
import com.rede.didiok.common.utils.StringUtils;


/**
 * 首页RestApi
 *
 * @author Rede
 * @date 2023年09月01日下午3:27:24
 */
@RestController
@RequestMapping("/admin/index")
@Api(value = "首页相关接口", tags = {"首页相关接口"})
@Slf4j
public class IndexRestApi {

    @Resource
    private RankFeignClient rankFeignClient;
    @Resource
    private UserFeignClient userFeignClient;
    @Resource
    private RedisUtil redisUtil;

    @ApiOperation(value = "首页初始化数据", notes = "首页初始化数据", response = String.class)
    @RequestMapping(value = "/init", method = RequestMethod.GET)
    public String init() {

        Map<String, Object> map = new HashMap<>(Constants.NUM_FOUR);

        // 缓存中获取数量
        String rankMemberCount = redisUtil.get(RedisConf.DASHBOARD + Constants.SYMBOL_COLON + RedisConf.RANK_MEMBER_COUNT);
        String memberCommentCount = redisUtil.get(RedisConf.DASHBOARD + Constants.SYMBOL_COLON + RedisConf.MEMBER_COMMENT_COUNT);

        // 缓存中存在就直接取
        if(StringUtils.isNotEmpty(rankMemberCount) && StringUtils.isNotEmpty(memberCommentCount)){
            map.put(SysConf.RANK_MEMBER_COUNT, Integer.valueOf(rankMemberCount));
            map.put(SysConf.MEMBER_COMMENT_COUNT, Integer.valueOf(memberCommentCount));
        // 缓存中不存在，查询数据库
        }else {
            // 调用远程服务didiok-rank的接口查询
            String countStr = rankFeignClient.getTotalCount();
            RankCountResultDto countResultDto = JSON.parseObject(countStr, RankCountResultDto.class);
            if (null != countResultDto && countResultDto.getRankMemberCount() != null) {
                // 榜单成员的数量
                redisUtil.setEx(RedisConf.DASHBOARD + Constants.SYMBOL_COLON + RedisConf.RANK_MEMBER_COUNT, String.valueOf(countResultDto.getRankMemberCount()), 10, TimeUnit.MINUTES);
                map.put(SysConf.RANK_MEMBER_COUNT, countResultDto.getRankMemberCount());
            }
            if (null != countResultDto && countResultDto.getMemberCommentCount() != null) {
                // 评论的数量
                redisUtil.setEx(RedisConf.DASHBOARD + Constants.SYMBOL_COLON + RedisConf.MEMBER_COMMENT_COUNT, String.valueOf(countResultDto.getMemberCommentCount()), 10, TimeUnit.MINUTES);
                map.put(SysConf.MEMBER_COMMENT_COUNT, countResultDto.getMemberCommentCount());
            }


        }

        // 缓存中获取数量
        String userCount = redisUtil.get(RedisConf.DASHBOARD + Constants.SYMBOL_COLON + RedisConf.USER_COUNT);
        String visitCount = redisUtil.get(RedisConf.DASHBOARD + Constants.SYMBOL_COLON + RedisConf.VISIT_COUNT);
        // 缓存中存在就直接取
        if(StringUtils.isNotEmpty(userCount) && StringUtils.isNotEmpty(visitCount)){
            map.put(SysConf.USER_COUNT, Integer.valueOf(userCount));
            map.put(SysConf.VISIT_COUNT, Integer.valueOf(visitCount));
        // 缓存中不存在，查询数据库
        }else {
            // 调用远程服务didiok-user的接口查询
            Map<String, String> resultMap = new HashMap<>();
            String countStr = userFeignClient.getTotalCount();
            UserCountResultDto countResultDto = JSON.parseObject(countStr, UserCountResultDto.class);

            if (null != countResultDto && countResultDto.getUserCount() != null) {
                // 用户的数量
                redisUtil.setEx(RedisConf.DASHBOARD + Constants.SYMBOL_COLON + RedisConf.USER_COUNT, String.valueOf(countResultDto.getUserCount()), 10, TimeUnit.MINUTES);
                map.put(SysConf.USER_COUNT, countResultDto.getUserCount());
            }
            if (null != countResultDto && countResultDto.getWebVisitCount() != null) {
                // 获取今日网站访问人数
                redisUtil.setEx(RedisConf.DASHBOARD + Constants.SYMBOL_COLON + RedisConf.VISIT_COUNT, String.valueOf(countResultDto.getWebVisitCount()), 10, TimeUnit.MINUTES);
                map.put(SysConf.VISIT_COUNT, countResultDto.getWebVisitCount());
            }
        }

        return ResultUtil.result(SysConf.SUCCESS, map);
    }

    @ApiOperation(value = "获取最近一周用户独立IP数和访问量", notes = "获取最近一周用户独立IP数和访问量", response = String.class)
    @RequestMapping(value = "/getVisitByWeek", method = RequestMethod.GET)
    public String getVisitByWeek() {
        // TODO  改成直接调用didiok-user的接口，而不是经过didiok-admin这一层了
        return userFeignClient.getWebVisitByWeek();
    }

    @ApiOperation(value = "获取每个rank榜单下的榜单成员数目", notes = "获取每个rank榜单下的榜单成员数目", response = String.class)
    @RequestMapping(value = "/getRankMemberCountByRank", method = RequestMethod.GET)
    public String getRankMemberCountByRank() {
        // TODO  改成直接调用didiok-rank的接口，而不是经过didiok-admin这一层了
        return rankFeignClient.getRankMemberCountByRank();
    }

    @ApiOperation(value = "获取每个榜单分类下的榜单数目", notes = "获取每个榜单类型下的榜单数目", response = String.class)
    @RequestMapping(value = "/getRankCountByCategory", method = RequestMethod.GET)
    public String getRankCountByCategory() {

        // TODO  改成直接调用didiok-rank里面的接口，而不是经过didiok-admin这一层了
        return rankFeignClient.getRankCountByCategory();
    }

    @ApiOperation(value = "获取一年内发布的榜单成员数", notes = "获取一年内发布的榜单成员数", response = String.class)
    @RequestMapping(value = "/getRankMemberContributeCount", method = RequestMethod.GET)
    public String getRankMemberContributeCount() {

        // TODO  改成直接调用didiok-rank里面的接口，而不是经过didiok-admin这一层了
        return rankFeignClient.getRankMemberContributeCount();
    }
}
